package com.gking.resourcePool.service.Project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.resourcePool.common.BaseContext;
import com.gking.resourcePool.common.Result;
import com.gking.resourcePool.entity.Project;
import com.gking.resourcePool.entity.ProjectGroup;
import com.gking.resourcePool.entity.Resource;
import com.gking.resourcePool.mapper.ProjectMapper;
import com.gking.resourcePool.service.ProjectGroup.ProjectGroupService;
import com.gking.resourcePool.service.Resource.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)//三级缓存解决bean循环注入问题
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private ProjectGroupService projectGroupService;

    @Autowired
    private ResourceService resourceService;


    /**
     * 新增项目
     *
     * @param name        项目名
     * @param description 项目描述
     * @return s
     */
    @Override
    public Result<String> addProject(String name, String description) {
        Project project = new Project();
        project.setName(name);
        project.setDescription(description);
        project.setUserId(BaseContext.getCurrentId());
        this.save(project);

        return Result.success("新增项目成功");
    }

    /**
     * 修改项目
     *
     * @param projectId   项目id
     * @param name        项目名
     * @param description 项目描述
     * @return s
     */
    @Override
    public Result<String> updateProject(String projectId, String name, String description) {
        LambdaUpdateWrapper<Project> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Project::getId, projectId);
        wrapper.eq(Project::getUserId, BaseContext.getCurrentId());
        wrapper.eq(Project::getIsDelete, 0);
        wrapper.set(Project::getName, name);
        wrapper.set(Project::getDescription, description);
        this.update(wrapper);

        return Result.success("修改项目成功");
    }

    /**
     * 删除项目
     *
     * @param projectId 项目id
     * @return s
     */
    @Override
    @Transactional
    public Result<String> deleteProject(String projectId) {
        LambdaUpdateWrapper<Project> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Project::getId, projectId);
        wrapper.eq(Project::getUserId, BaseContext.getCurrentId());
        wrapper.eq(Project::getIsDelete, 0);
        wrapper.set(Project::getIsDelete, 1);
        wrapper.set(Project::getDeleteTime, LocalDateTime.now());
        wrapper.set(Project::getDeleteUserId, BaseContext.getCurrentId());
        this.update(wrapper);

        LambdaUpdateWrapper<ProjectGroup> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProjectGroup::getProjectId, projectId);
        updateWrapper.eq(ProjectGroup::getUserId, BaseContext.getCurrentId());
        updateWrapper.eq(ProjectGroup::getIsDelete, 0);
        updateWrapper.set(ProjectGroup::getIsDelete, 1);
        updateWrapper.set(ProjectGroup::getDeleteTime, LocalDateTime.now());
        updateWrapper.set(ProjectGroup::getDeleteUserId, BaseContext.getCurrentId());
        projectGroupService.update(updateWrapper);

        LambdaUpdateWrapper<Resource> resourceWrapper = new LambdaUpdateWrapper<>();
        resourceWrapper.eq(Resource::getProjectId, projectId);
        resourceWrapper.eq(Resource::getUserId, BaseContext.getCurrentId());
        resourceWrapper.eq(Resource::getIsDelete, 0);
        resourceWrapper.set(Resource::getIsDelete, 1);
        resourceWrapper.set(Resource::getDeleteTime, LocalDateTime.now());
        resourceWrapper.set(Resource::getDeleteUserId, BaseContext.getCurrentId());
        resourceService.update(resourceWrapper);

        return Result.success("删除项目成功");
    }

    /**
     * 获取项目列表
     *
     * @return l
     */
    @Override
    public Result<List<Project>> getProjects() {
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Project::getUserId, BaseContext.getCurrentId());
        wrapper.eq(Project::getIsDelete, 0);
        wrapper.orderByDesc(Project::getCreateTime);
        List<Project> list = this.list(wrapper);

        return Result.success("获取项目列表成功", list);
    }
}
